package com.yami.shop.cp.comment.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yami.shop.bean.app.dto.ShopCartDto;
import com.yami.shop.bean.event.ShopCartEvent;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.Json;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.cp.comment.dao.CollaborativePurchaseBasketMapper;
import com.yami.shop.cp.comment.dto.CPProdInfo;
import com.yami.shop.cp.comment.dto.CollaborativePurchaseShopCartItemDto;
import com.yami.shop.cp.comment.model.CollaborativePurchaseBasket;
import com.yami.shop.cp.comment.param.ChangeSubmitPersonShopCartParam;
import com.yami.shop.cp.comment.service.CollaborativePurchaseBasketService;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 协同采购提报人购物车 服务实现类
 * </p>
 *
 * @author xch
 * @since 2021-06-29
 */
@Service
@AllArgsConstructor
public class CollaborativePurchaseBasketServiceImpl extends ServiceImpl<CollaborativePurchaseBasketMapper, CollaborativePurchaseBasket> implements CollaborativePurchaseBasketService {

    private final CollaborativePurchaseBasketMapper collaborativePurchaseBasketMapper;
    private final ApplicationContext applicationContext;

    @Override
    public List<CollaborativePurchaseShopCartItemDto> getSubmitPersonShopCartItems(String mobile, String collaborativePurchaseId) {
        List<CollaborativePurchaseShopCartItemDto> collaborativePurchaseShopCartItemDtoList = collaborativePurchaseBasketMapper.getSubmitPersonShopCartItems(mobile, collaborativePurchaseId, I18nMessage.getDbLang());
        for (CollaborativePurchaseShopCartItemDto collaborativePurchaseShopCartItemDto : collaborativePurchaseShopCartItemDtoList) {
            // 物流配送信息
            Product.DeliveryModeVO deliveryModeVO = Json.parseObject(collaborativePurchaseShopCartItemDto.getDeliveryMode(), Product.DeliveryModeVO.class);
            collaborativePurchaseShopCartItemDto.setDeliveryModeVO(deliveryModeVO);
            collaborativePurchaseShopCartItemDto.setProductTotalAmount(Arith.mul(collaborativePurchaseShopCartItemDto.getProdCount(), collaborativePurchaseShopCartItemDto.getPrice()));
        }
        return collaborativePurchaseShopCartItemDtoList;
    }

    @Override
    public void addSubmitPersonShopCartItem(ChangeSubmitPersonShopCartParam param, String mobile) {
        CollaborativePurchaseBasket basket = new CollaborativePurchaseBasket();
        basket.setShopId(param.getShopId());
        basket.setProdId(param.getProdId());
        basket.setSkuId(param.getSkuId());
        basket.setBasketCount(param.getCount());
        basket.setMobile(mobile);
        basket.setCollaborativePurchaseId(param.getCollaborativePurchaseId());
        collaborativePurchaseBasketMapper.insert(basket);
    }

    @Override
    public void deleteSubmitPersonShopShopCartItemsByBasketIds(List<Long> basketIds) {
        collaborativePurchaseBasketMapper.deleteBatchIds(basketIds);
    }

    @Override
    public List<CollaborativePurchaseShopCartItemDto> getShopCartItemsByCollaborativePurchaseId(String collaborativePurchaseId) {
        List<CollaborativePurchaseShopCartItemDto> collaborativePurchaseShopCartItemDtoList = collaborativePurchaseBasketMapper.getShopCartItemsByCollaborativePurchaseId(collaborativePurchaseId);
        for (CollaborativePurchaseShopCartItemDto collaborativePurchaseShopCartItemDto : collaborativePurchaseShopCartItemDtoList) {
            // 物流配送信息
            Product.DeliveryModeVO deliveryModeVO = Json.parseObject(collaborativePurchaseShopCartItemDto.getDeliveryMode(), Product.DeliveryModeVO.class);
            collaborativePurchaseShopCartItemDto.setDeliveryModeVO(deliveryModeVO);
            collaborativePurchaseShopCartItemDto.setProductTotalAmount(Arith.mul(collaborativePurchaseShopCartItemDto.getProdCount(), collaborativePurchaseShopCartItemDto.getPrice()));
        }
        return collaborativePurchaseShopCartItemDtoList;
    }

    @Override
    public IPage<CollaborativePurchaseShopCartItemDto> pageShopCartItemsByCollaborativePurchaseId(PageParam page, String collaborativePurchaseId, String status) {
        IPage<CollaborativePurchaseShopCartItemDto> collaborativePurchaseShopCartItemDtoIPage = collaborativePurchaseBasketMapper.getPageShopCartItemsByCollaborativePurchaseId(page, collaborativePurchaseId, status);
        for (CollaborativePurchaseShopCartItemDto collaborativePurchaseShopCartItemDto : collaborativePurchaseShopCartItemDtoIPage.getRecords()) {
            // 物流配送信息
            Product.DeliveryModeVO deliveryModeVO = Json.parseObject(collaborativePurchaseShopCartItemDto.getDeliveryMode(), Product.DeliveryModeVO.class);
            collaborativePurchaseShopCartItemDto.setDeliveryModeVO(deliveryModeVO);
            collaborativePurchaseShopCartItemDto.setProductTotalAmount(Arith.mul(collaborativePurchaseShopCartItemDto.getProdCount(), collaborativePurchaseShopCartItemDto.getPrice()));
        }
        return collaborativePurchaseShopCartItemDtoIPage;
    }

    @Override
    public List<CollaborativePurchaseShopCartItemDto> getShopCartItemsByCollaborativePurchaseIdAndStatus(String collaborativePurchaseId, String status) {
        List<CollaborativePurchaseShopCartItemDto> collaborativePurchaseShopCartItemDtoList = collaborativePurchaseBasketMapper.getPageShopCartItemsByCollaborativePurchaseId(collaborativePurchaseId, status);
        for (CollaborativePurchaseShopCartItemDto collaborativePurchaseShopCartItemDto : collaborativePurchaseShopCartItemDtoList) {
            // 物流配送信息
            Product.DeliveryModeVO deliveryModeVO = Json.parseObject(collaborativePurchaseShopCartItemDto.getDeliveryMode(), Product.DeliveryModeVO.class);
            collaborativePurchaseShopCartItemDto.setDeliveryModeVO(deliveryModeVO);
            collaborativePurchaseShopCartItemDto.setProductTotalAmount(Arith.mul(collaborativePurchaseShopCartItemDto.getProdCount(), collaborativePurchaseShopCartItemDto.getPrice()));
        }
        return collaborativePurchaseShopCartItemDtoList;
    }

    @Override
    public List<ShopCartDto> getShopCarts(List<CollaborativePurchaseShopCartItemDto> collaborativePurchaseSubmitShopCartItems) {
        // 根据店铺ID划分item
        Map<Long, List<CollaborativePurchaseShopCartItemDto>> shopCartMap = collaborativePurchaseSubmitShopCartItems.stream().collect(Collectors.groupingBy(CollaborativePurchaseShopCartItemDto::getShopId));

        // 返回一个店铺的所有信息
        List<ShopCartDto> shopCartDtos = Lists.newArrayList();
        for (Long shopId : shopCartMap.keySet()) {

            //获取店铺的所有商品项
            List<CollaborativePurchaseShopCartItemDto> shopCartItemDtoList = shopCartMap.get(shopId);

            // 构建每个店铺的购物车信息
            ShopCartDto shopCart = new ShopCartDto();

            //店铺信息
            shopCart.setShopId(shopId);
            shopCart.setShopName(shopCartItemDtoList.get(0).getShopName());
            // 着急测试，店铺优惠暂时去掉
        }

        return shopCartDtos;
    }

    @Override
    public List<CPProdInfo> getCPSubmitAndCompletePordInfo(String cpId) {
        return collaborativePurchaseBasketMapper.getCPSubmitAndCompletePordInfo(cpId);
    }
}
